/*
 * @Description:
 * @Author: fhw
 * @Date: 2022-08-09 17:20:15
 */
import { equals, isNil } from 'ramda';
import { Server } from 'typeorm';

export interface EnvConfig {
  [key: string]: string;
}

export class ConfigService {
  public users = new Map(); // 用户
  private room = new Map(); // 群聊
  public socketio: Server;
  // 消息状态类型
  public sysMessageStatus: {
    0: '未通过';
    1: '通过';
    2: '作废';
  };
  /**
   * @param userId 用户id
   * @param socketId 聊天id
   */
  setChatUser(userId: string, socketId: string) {
    this.users.set(userId, socketId);
  }
  /**
   * @param userId 用户id
   * @returns string
   */
  getChatUser(userId: string | number) {
    if (isNil(userId)) return;
    return this.users.get(userId);
  }
  /**
   * @param userId 用户id
   * @returns string
   */
  delChatUser(userId: string | number) {
    if (isNil(userId)) return;
    this.users.delete(userId);
  }
  setSocketio(server: Server) {
    this.socketio = server;
  }
  /**
   * @param roomId 群聊id
   * @param userId 聊天id
   */
  setRoom(roomId: string, userId: string) {
    const room = this.getRoom(roomId) ?? [userId]
    if (!room.includes(userId)) {
      room.push(userId)
    }
    this.room.set(roomId, room);
  }
  /**
   * @param roomId 群聊id
   * @returns {Array}
   */
  getRoom(roomId: string | number): string[] {
    if (isNil(roomId)) return;
    return this.room.get(roomId);
  }
  /**
   * @param roomId 群聊id
   * @returns string
   */
  delRoom(roomId: string | number) {
    if (isNil(roomId)) return;
    this.users.delete(roomId);
  }
  /**
  * @param roomId 群聊id
  * @param userId 聊天id
  */
  delRoomUserId(roomId: string, userId: string) {
    const room = this.getRoom(roomId) ?? []
    const index = room.indexOf(userId)
    if (equals(index, -1)) return
    room.splice(index, 1)
    this.room.set(roomId, room);
  }
}
